package com.hertz.alarm.controller.api;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hertz.alarm.controller.BaseController;
import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.MatchNba;
import com.hertz.alarm.entity.Team;
import com.hertz.alarm.service.MatchNbaService;
import com.hertz.alarm.service.TeamNbaService;
import com.hertz.alarm.util.Page;
import com.hertz.alarm.warper.MatchNbaWarper;
@Controller
@RequestMapping("/api/match/nba")
public class ApiMatchController extends BaseController{

	@Resource
	private MatchNbaService matchNbaService;
	@Resource
	private TeamNbaService teamNbaService;
	@RequestMapping(value="getById",method=RequestMethod.GET)
	public @ResponseBody ResultModel<Object> getById(
			@RequestParam(required=true,value="id") Long id){
		MatchNba matchNba = matchNbaService.getById(id);
		return buildResultModel(matchNba);
	}
	
	@RequestMapping(value="getBySourceId",method=RequestMethod.GET)
	public @ResponseBody ResultModel<Object> getBySourceId(@RequestParam(required=true,value="sourceId") String sourceId){
		MatchNba matchNba = matchNbaService.getBySourceId(sourceId);
		return buildResultModel(matchNba);
	}
	
	@RequestMapping(value="getByYmd",method=RequestMethod.GET)
	public @ResponseBody ResultModel<Object> getByYmd(
			@RequestParam(required=true,value="ymd") String ymd){
		List<MatchNba> list = matchNbaService.listByYmd(ymd);
		ResultModel<Object> resultModel = null;
		if(list != null){
			Page<MatchNbaWarper> page = new Page<MatchNbaWarper>();
			List<MatchNbaWarper> warperList = new ArrayList<MatchNbaWarper>();
			for(MatchNba matchNba : list){
				MatchNbaWarper warper = new MatchNbaWarper();
				warper.setMatchNba(matchNba);
				Team hostTeam = teamNbaService.getTeamBySourceId(String.valueOf(matchNba.getHostTeamId()));
				warper.setHostTeam(hostTeam);
				Team guestTeam = teamNbaService.getTeamBySourceId(String.valueOf(matchNba.getGuestTeamId()));
				warper.setGuestTeam(guestTeam);
				warperList.add(warper);
			}
			page.setResult(warperList);
			page.setTotalCount(list.size());
			resultModel = buildResultModel(page);
		}else
			resultModel = buildResultModel(null);
		return resultModel;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
